from PIL import Image, ImageDraw

def resize_image_for_display(image, max_width, max_height):
    """调整图像大小以适应显示区域."""
    width, height = image.size
    if width > max_width or height > max_height:
        ratio = min(max_width / width, max_height / height)
        new_width = int(width * ratio)
        new_height = int(height * ratio)
        return image.resize((new_width, new_height), Image.LANCZOS)
    return image

def draw_detection_boxes(image, boxes, scores, classes, confidence_threshold=0.5):
    """在图像上绘制检测框."""
    draw = ImageDraw.Draw(image)
    width, height = image.size

    for i in range(len(boxes)):
        if scores[i] > confidence_threshold:  # 只绘制置信度高于阈值的框
            ymin, xmin, ymax, xmax = boxes[i]
            left = int(xmin * width)
            top = int(ymin * height)
            right = int(xmax * width)
            bottom = int(ymax * height)

            # 绘制矩形框
            draw.rectangle([(left, top), (right, bottom)], outline="red", width=2)

            # 添加标签 (这里只是一个示例，你需要根据你的classes定义来映射类别)
            class_name = f"Class {classes[i]}"  # 简单的类别名称
            label = f"{class_name}: {scores[i]:.2f}"
            draw.text((left, top - 12), label, fill="red")  # 调整文本位置

    return image